const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const Lesson = sequelize.define('Lesson', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  course_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '课程ID'
  },
  title: {
    type: DataTypes.STRING(200),
    allowNull: false,
    comment: '课时标题'
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '课时描述'
  },
  content: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '课时内容'
  },
  video_url: {
    type: DataTypes.STRING(500),
    allowNull: true,
    comment: '视频链接'
  },
  replay_url: {
    type: DataTypes.STRING(500),
    allowNull: true,
    comment: '回放链接'
  },
  duration: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '课时时长(秒)'
  },
  sort_order: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '排序'
  },
  is_free: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
    comment: '是否免费'
  },
  status: {
    type: DataTypes.ENUM('draft', 'published', 'archived'),
    defaultValue: 'draft',
    comment: '状态'
  },
  attachments: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: '附件信息'
  },
  subtitles: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: '字幕信息'
  },
  created_at: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
    comment: '创建时间'
  },
  updated_at: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
    comment: '更新时间'
  }
}, {
  tableName: 'lessons',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    {
      fields: ['course_id']
    },
    {
      fields: ['status']
    },
    {
      fields: ['sort_order']
    },
    {
      fields: ['is_free']
    }
  ]
});

// 定义关联关系
Lesson.associate = (models) => {
  Lesson.belongsTo(models.Course, {
    foreignKey: 'course_id',
    as: 'course'
  });
};

module.exports = Lesson; 